﻿#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using Turnota.Map;
using Turnota.Input;
using Turnota.Pathfinding;
#endregion

namespace Turnota
{
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        public static SpriteBatch spriteBatch;
        public static ContentManager contentManager;
        private Color _backgroundColor = new Color(145, 225, 242);
        private Map.Map _currentMap;
        private KeyboardHelper _keyboard;
        private MouseHelper _mouse;
        private Pathfinder pathFinder;
        private List<Vector2> path;

        private Texture2D pathImage;

        public Game1()
            : base()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferHeight = 768;
            graphics.PreferredBackBufferWidth = 1024;
        }

        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            contentManager = Content;

            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            _currentMap = new Map.Map();
            _keyboard = new KeyboardHelper(new List<Keys> { Keys.Left, Keys.Right, Keys.Up, Keys.Down, Keys.O, Keys.P, Keys.L, Keys.S });
            _mouse = new MouseHelper();
            _mouse.MouseButtonReleased += _mouse_MouseButtonReleased;
            _keyboard.KeyPressed += _keyboard_KeyPressed;
            _mouse.MouseMoved += _mouse_MouseMoved;

            pathImage = contentManager.Load<Texture2D>("TileBlocked");

            pathFinder = new Pathfinder(_currentMap);
            // TODO: use this.Content to load your game content here
        }

        void _mouse_MouseMoved(MouseHelper sender, MouseState state)
        {
            Point p = _currentMap.ConvertScreenPositionToTileIndex(new Vector2(state.X, state.Y));
            path = pathFinder.FindPath(_currentMap.selectedTileIndex, p);
        }

        private void DrawPath()
        {
            foreach (Vector2 pathIndex in path)
            {
                Vector2 position = _currentMap.tiles[(int)pathIndex.X, (int)pathIndex.Y].Position;
                Game1.spriteBatch.Draw(pathImage, position, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0.3f);
            }

        }


        private void _mouse_MouseButtonReleased(MouseHelper sender, MouseButtons button, MouseState state)
        {
            Point p = _currentMap.ConvertScreenPositionToTileIndex(new Vector2(state.X, state.Y));
            _currentMap.SetSelector(p.X, p.Y);
        }

        void _keyboard_KeyPressed(KeyboardHelper sender, Keys key)
        {
            if (key == Keys.Down)
                _currentMap.MoveSelector(0, -1);
            else if (key == Keys.Up)
                _currentMap.MoveSelector(0, 1);
            else if (key == Keys.Left)
                _currentMap.MoveSelector(-1, 0);
            else if (key == Keys.Right)
                _currentMap.MoveSelector(1, 0);
            else if (key == Keys.O)
                _currentMap.ModifyTileHeight(1);
            else if (key == Keys.P)
                _currentMap.ModifyTileHeight(-0.5f);
            else if (key == Keys.S)
                _currentMap.Save();
            else if (key == Keys.L)
                _currentMap.Load();
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            // TODO: Add your update logic here
            _keyboard.Update(gameTime);
            _mouse.Update();

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            
            GraphicsDevice.Clear(_backgroundColor);

           
            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend); 

            _currentMap.Draw();


            DrawPath();

            spriteBatch.End();

            // TODO: Add your drawing code here


            base.Draw(gameTime);
        }
    }
}
